<?php
//require section
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_news.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_upload.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';
$title = 'Admin Panel';

$id = 0;
$title = "";
$content = "";
$cat_news_id = "";
$image = "";
$action = "Add";
$action_show = "Thêm";

if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_news::get_news_by_id($id);
    $id = $edit_item['news_id'];
    $title = $edit_item['news_title'];
    $content = $edit_item['news_content'];
    $cat_news_id = $edit_item['cat_news_id'];
    $image       = $edit_item['news_img_url'];
    $action = "Update";
    $action_show = "Sửa";
} else if (!empty($_POST)) {
    $id_save = "";
    if (!empty($_POST['id'])) {
        $id_save = $_POST['id'];
    }
    $title_save = "";
    if (!empty($_POST['txt_title'])) {
        $title_save = $_POST['txt_title'];
    }
    $content_save = "";
    if (!empty($_POST['txt_content'])) {
        $content_save = $_POST['txt_content'];
    }
    $address = "";
    if (!empty($_POST['ddl_cat_id'])) {
        $address = $_POST['ddl_cat_id'];
    }
    $image_save = "";
    if (!empty($_FILES['f_image']['size'])) {
        $lib_upload = new lib_upload();
        if ($lib_upload->upload_file('f_image', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_IMAGES_NEWS)) {
            $image_save = $_FILES['f_image']['name'];
        }
    } else if (!empty($_POST['hidd_image'])) {
        $image_save = $_POST['hidd_image'];
    }
    
    
    if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {
        $error_show = api_news::validate_news_fields($title_save, $content_save, $address, $image_save);
        if (empty($error_show)) {
            if (api_news::save_news($id_save, $title_save, $content_save, $address, $image_save)) {
                $success_info = "Thêm tin tức &lt;".$title_save."&gt; thành công.";
                if ($id_save > 0) {
                    $success_info = "Sửa tin tức &lt;".$title_save."&gt; thành công.";
                }
            }
        } else {
            $id = $id_save;
            $title = $title_save;
            $content = $content_save;
            $cat_news_id = $address;
            $image       = $image_save;
            $action = $_POST['Action'];
            $action_show = "Sửa";
            if ($action === "Add") {
                $action_show = "Thêm";
            }
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
        $id_delete = 0;
        if (!empty($_POST['delete_id'])) {
            $id_delete = $_POST['delete_id'];
        }
        api_news::delete_news($id_delete);
        $success_info = "Xóa tin tức thành công.";
    }
}



?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="uk-container uk-container-center uk-margin-top">
            <?php
            require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
            ?>

            <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">
                    <?php
                    if (!empty($error_show)) {?>
                    <div class="error">Lỗi: <?php echo $error_show; ?></div>
                    <?php
                    } else if (!empty($success_info)) {?>
                    <div class="success"><?php echo $success_info; ?></div>
                    <?php
                    }?>

                <form action="?" method="POST" id="form_delete" >
                    <input type="hidden" name="delete_id" value="0" />
                    <input type="hidden" name="Action" value="Delete" />
                </form>
                <form name="form_data" id="form_data" method="POST"
                          action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"
                          enctype="multipart/form-data"
                          class="uk-form uk-width-medium-1-1">

                    <fieldset>
                    <legend>Tin Tức</legend>

                        <div class="ui form segment form-background">
                            <input type="hidden" name="id" value="<?php echo $id; ?>" />

                            <div class="uk-form-row">
                                <label for="Title">Tiêu đề <span class="required">*</span></label>
                                <input id="Title" name="txt_title" value="<?php echo $title ?>" placeholder="" type="text">
                            </div>

                            <div class="uk-form-row">
                                <label for="Description">Loại tin tức</label>
                                <select name="ddl_cat_id">
                                    <?php
                                    $list = api_news::get_all_news_categories();

                                    foreach ($list as $i => $item) {
                                        ?>
                                        <option value="<?php echo $item['cat_news_id'] ?>"
                                        <?php
                                        if (!empty($edit_article_old)) {

                                            if ($item['cat_news_id'] == $cat_news_id) {
                                                echo 'selected';
                                            }
                                        }
                                        ?>
                                                >
                                            <?php echo $item['cate_news_name'] ?> </option>
                                    <?php

                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="uk-form-row">
                                <label for="Poster">Hình ảnh minh họa <span class="required">*</span></label>

                                <input type="file" name="f_image"/>
                                <?php
                                    if (!empty($image)) {
                                        ?>
                                        <input type="hidden" name="hidd_image" value="<?php echo $image ?>"/>
                                        <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_NEWS . $image; ?>"
                                             style="max-height: 100px;">
                                    <?php
                                    } ?>
                            </div>
                            
                            <div class="uk-form-row">
                                <label for="Description">Nội dung <span class="required">*</span></label>
                                <textarea id="Description" class="ckeditor" name="txt_content" placeholder="Detail" class="form-control" rows="3"><?php echo $content; ?></textarea>
                            </div>

                            <div class="uk-form-row">
                                <input type="hidden" name="Action" value="<?php echo $action; ?>">
                                <button class="uk-button uk-button-primary" id="action-button" onclick="submit_data();"><?php echo $action_show; ?></button>
                                <a href="?">Hủy</a>
                            </div>

                        </div>
                    </fieldset>
                        </form>


                <hr/>
                <h3><strong>Danh sách</strong></h3>

                <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
                                <thead>
                                    <tr>
                                        <th>Tiêu đề</th>
                                        <th>Loại tin tức</th>
                                        <th>Nội dung</th>
                                        <th>Thao tác</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $current_page = 1;
                                    $page_size = 10;
                                    if (!empty($_GET['p'])) {
                                        $current_page = $_GET['p'];
                                    }

                                    $list_total = api_news::get_all_news();
                                    $lib_pager = new lib_pager();
                                    $total_page = $lib_pager->get_total_page($list_total, $page_size);
                                    $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                                    $count = 0;
                                    if (empty($current_page_list) || count($current_page_list) == 0) {
                                        echo "<tr><td>Chưa có dữ liệu</td></tr>";
                                    } else {
                                        foreach ($current_page_list as $i => $item) {?>
                                        <tr>
                                            <td><?php echo $item['news_title']; ?></td>
                                            <td><?php echo $item['news_cat_name']; ?></td>
                                            <td><?php echo substr(strip_tags($item['news_content']), 0, 100); ?></td>
                                            <td>
                                                <a href="?Action=Edit&Id=<?php echo $item['news_id']; ?>">
                                                <i class="uk-icon-edit"></i>
                                                </a>
                                                <i class="uk-icon-eraser" onclick="confirmDelete('<?php echo $item['news_id']; ?>')"></i>
                                            </td>
                                        </tr>
                                        <?php
                                        }
                                    } ?>
                                    <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                        <td colspan="7">
                                            <table>
                                                <tbody>
                                                    <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                                        <td colspan="7">
                                                            <table>
                                                                <tbody>
                                                                    <tr>
                                                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                                                            </td>
                                                                        <?php } ?>
                                                                    </tr>
                                                                </tbody>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
            </section>

            <?php
            require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
            ?>
        </div>
    </body>
</html>
